package pds

import (
	"errors"
	"net"
	"time"
	log "ac-common-go/glog"
	"ac-julink/rpcagent"
	"ac-julink/tool/config"
	"ac-julink/tool/util"

	"git.apache.org/thrift.git/lib/go/thrift"
)

var thriftPoolManager *rpcagent.ThriftPoolManager

func dial(ip, port string, connTimeout time.Duration) (*rpcagent.IdleClient, error) {
	tSocket, err := thrift.NewTSocketTimeout(net.JoinHostPort(ip, port), connTimeout)
	if err != nil {
		log.Errorf("connect to pds error:%s", err)
		return nil, err
	}
	if err := tSocket.Open(); err != nil {
		return nil, err
	}
	transport := thrift.NewStreamTransportFactory(tSocket, tSocket, true).GetTransport(tSocket)
	//	transport := thrift.NewTTransportFactory().GetTransport(tSocket)
	//	transport := thrift.NewTBufferedTransportFactory(1024).GetTransport(tSocket)
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()

	client := NewPdsClientFactory(transport, protocolFactory)

	return &rpcagent.IdleClient{Socket: tSocket, Client: client, CreateTime: time.Now()}, nil
}
func close(c *rpcagent.IdleClient) error {
	err := c.Socket.Close()
	return err
}
func InitTriftPool() {
	defer func() {
		if pErr := recover(); pErr != nil {
			log.Errorf("PDS InitTriftPool panic:%+v\n%s", pErr, util.PanicTrace())
		}
	}()
	ip := config.GetConfigValue("WGNS", "rpc_pds_ip")
	if ip == "" {
		log.Errorf("rpc_pds_ip is not valid!!")
		return
	}
	port := config.GetConfigValue("WGNS", "rpc_pds_port")
	if port == "" {
		log.Errorf("rpc_pds_port is not valid!!")
		return
	}
	maxConn, ok := config.GetConfigIntValue("WGNS", "rpc_pds_maxconn")
	if !ok {
		maxConn = rpcagent.DEFAULTMAXCONN
	}
	initConn, ok := config.GetConfigIntValue("WGNS", "rpc_pds_initconn")
	if !ok {
		initConn = rpcagent.DEFAULTINITCONN
	}
	connTimeout, ok := config.GetConfigIntValue("WGNS", "rpc_pds_conntimeout")
	if !ok {
		connTimeout = rpcagent.DEFAULTCONNTIMEOUT
	}
	idleTimeout, ok := config.GetConfigIntValue("WGNS", "rpc_pds_idletimeout")
	if !ok {
		idleTimeout = rpcagent.DEFAULTIDLETIME
	}
	retryNum, ok := config.GetConfigIntValue("WGNS", "rpc_pds_retrynum")
	if !ok {
		retryNum = rpcagent.DEFAULTRETRYNUM
	}
	failMax, ok := config.GetConfigIntValue("WGNS", "rpc_pds_failmax")
	if !ok {
		failMax = rpcagent.DEFAULTFAILNUM
	}
	thriftPoolManager = rpcagent.NewThriftPoolManager(ip, port, maxConn, initConn,
		connTimeout, idleTimeout, dial, close, retryNum, failMax)
	if thriftPoolManager == nil {
		log.Errorf("pds thrift poll manager init error!!!")
	}
}
func GetSysPara(SysParaName string) (*GetSysParaResult_, error) {
	log.Infof("Rpc call GetPGetSysParareKeyt ==>>%s", SysParaName)
	if thriftPoolManager == nil {
		return nil, errors.New("pds pool manager is nil")
	}
	args := make([]interface{}, 1)
	args[0] = SysParaName
	result, err := thriftPoolManager.AskRpc("GetSysPara", args)
	if err != nil {
		return nil, err
	}
	if result == nil {
		log.Errorf("GetSysPara result is nil, param:%s", SysParaName)
		return nil, errors.New("result is nil")
	}
	b, ok := result.(*GetSysParaResult_)
	if !ok {
		return nil, errors.New("interface to GetSysParaResult_ failed")
	}
	return b, nil
}
